/**
 * @Author MrsGuo
 * version 1.0
 * @Date 2023-08-11 16:51
 */
package com.cl.erp_2301.controller;

import com.cl.erp_2301.Service.CheckReturnService;
import com.cl.erp_2301.Service.ReturnOrdersService;
import com.cl.erp_2301.Service.ReturnService;
import com.cl.erp_2301.dto.*;
import com.cl.erp_2301.mapper.ReturnMapper;
import com.cl.erp_2301.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v2/returnorders")
public class ReturnController {
    @Resource
    private ReturnService returnService;
    @Resource
    private ReturnOrdersService returnOrdersService;
    @Resource
    private CheckReturnService checkReturnService;
    @GetMapping("/listByPage")
    public Result selectReturnOrders(Integer state, int page, int rows) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<ReturnDTO> returnOrdersDTOS = returnOrdersService.selectReturnRequestList(state);
        DataDTO data=new DataDTO();
        if (state==0) {
            for (ReturnDTO returnDTO : returnOrdersDTOS) {
                List<ReturnDTO> returnDetailDTOList = returnOrdersService.selectReturnDetailByUuid(returnDTO.getUuid());
                returnDTO.setReturnOrderDetailList(returnDetailDTOList);
            }
            data.setRows(returnOrdersDTOS);
        }
        if (state==1){
            for (ReturnDTO returnDTO : returnOrdersDTOS) {
                List<ReturnDTO> returnOutDTOS=returnOrdersService.selectReturnOutByUuid(returnDTO.getUuid());
            returnDTO.setReturnOrderDetailList(returnOutDTOS);
            }
            data.setRows(returnOrdersDTOS);
        }
        data.setRows(returnOrdersDTOS);
        data.setTotal(page1.getTotal());
        return Result.ok(data);
    }
    @PostMapping("/add")
    public Result addMarket(@RequestBody ReturnDetailDTO returnOrderDetails, HttpServletRequest request, HttpServletResponse response) {
        Date date = new Date();
        Integer supplieruuid = returnOrderDetails.getSupplieruuid();
        //遍历前端数据放入returnOrder表
        for (ReturnDTO returnAddDTO : returnOrderDetails.getReturnOrderDetails()) {
            if (returnAddDTO.getNum()==null){
                return Result.error(400, "添加个数为0，添加错误");
            }
            Integer storeNum=checkReturnService.selectStoreNum(returnAddDTO.getGoodsuuid());
            Integer num=returnAddDTO.getNum();
            if (num>storeNum){
                return Result.error(400,"库存不足，出库失败,库存大小为"+storeNum);
            }
           returnAddDTO.setCreatetime(date);
           returnAddDTO.setCreatername((String) request.getSession().getAttribute("name"));
           returnAddDTO.setType(returnService.selectTypeByGoodsuuid(returnAddDTO.getGoodsuuid()));
           returnAddDTO.setSupplieruuid(supplieruuid);
           returnAddDTO.setStoreuuid(returnService.selectStoreId(returnAddDTO.getGoodsuuid()));
        }

         Integer i=returnService.addReturnOrders(returnOrderDetails.getReturnOrderDetails());

        if (i==0){
            return Result.error(400,"添加错误");
        }
        return Result.ok();
    }
    @PostMapping("/doCheck")
    public Result checkReturn(@RequestParam("id") Integer id,HttpServletRequest request) throws ParseException {
      ReturnDTO returnOrders=new ReturnDTO();
        Date date=new Date();
      returnOrders.setChecktime(date);
      returnOrders.setEndtime(date);
      returnOrders.setCheckername((String) request.getSession().getAttribute("name"));
      returnOrders.setEndername((String) request.getSession().getAttribute("name"));
      returnOrders.setUuid(id);
        checkReturnService.checkReturn(returnOrders);
        checkReturnService.updateState(id);
        //库存oper
        StoreOperDTO storeOpDTO=checkReturnService.selectStoreOper(id);
      storeOpDTO.setOpertime(date);
        checkReturnService.insertStoreOper(storeOpDTO);
        //库存detail
        Integer storeNum=checkReturnService.selectStoreNum(storeOpDTO.getGoodsuuid());
        //出库的数量
        Integer num=storeOpDTO.getNum();
            Integer newNum=storeNum-num;
            StoreOperDTO storeDetailDTO=new StoreOperDTO();
            storeDetailDTO.setGoodsuuid(storeOpDTO.getGoodsuuid());
            storeDetailDTO.setNum(newNum);
            checkReturnService.updateNum(storeDetailDTO);

      return Result.ok();
    }
   @GetMapping("salesReturnlistByPage")
    public Result salesReturnList(int page, int rows){
       Page<Object> page1 = PageHelper.startPage(page, rows);
       List<MarketDTO> returnOrder = returnOrdersService.selectReturnInquiry();
       DataDTO data = new DataDTO();
            for (MarketDTO orderInquiryDTO:returnOrder) {
                List<ReturnDTO> returnDetailDTOList = returnOrdersService.selectReturnDetailByUuid(orderInquiryDTO.getUuid());
              orderInquiryDTO.setOrderDetailList(returnDetailDTOList);
            }
            data.setRows(returnOrder);
            data.setTotal(page1.getTotal());
            return Result.ok(data);
    }
}
